package com.elai.overview.controller;

import java.util.List;

import com.elai.common.core.domain.model.LoginUser;
import com.elai.common.utils.DateUtils;
import com.elai.common.utils.SecurityUtils;
import com.elai.overview.domain.MyOverviewItems;
import com.elai.overview.service.IMyOverviewItemsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.enums.BusinessType;
import com.elai.overview.domain.MyOverviewPage;
import com.elai.overview.service.IMyOverviewPageService;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.common.core.page.TableDataInfo;

/**
 * 概览页面Controller
 *
 * @author elai
 * @date 2021-08-29
 */
@RestController
@RequestMapping("/overview/page")
public class MyOverviewPageController extends BaseController
{
    @Autowired
    private IMyOverviewPageService myOverviewPageService;

    @Autowired
    private IMyOverviewItemsService myOverviewItemsService;
    /**
     * 查询概览页面列表
     */
    @PreAuthorize("@ss.hasPermi('overview:page:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyOverviewPage myOverviewPage)
    {
        LoginUser user= SecurityUtils.getLoginUser();
        myOverviewPage.setUserId(user.getUser().getUserId());
        List<MyOverviewPage> list = myOverviewPageService.selectMyOverviewPageList(myOverviewPage);
        return getDataTable(list);
    }

    @GetMapping("/publicOverviewPage")
    public TableDataInfo publicOverviewPage(MyOverviewPage myOverviewPage)
    {
        myOverviewPage.setPublicFlag("Y");
        List<MyOverviewPage> list = myOverviewPageService.selectMyOverviewPageList(myOverviewPage);
        return getDataTable(list);
    }

    /**
     * 导出概览页面列表
     */
    @PreAuthorize("@ss.hasPermi('overview:page:export')")
    @Log(title = "概览页面", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyOverviewPage myOverviewPage)
    {
        List<MyOverviewPage> list = myOverviewPageService.selectMyOverviewPageList(myOverviewPage);
        ExcelUtil<MyOverviewPage> util = new ExcelUtil<MyOverviewPage>(MyOverviewPage.class);
        return util.exportExcel(list, "概览页面数据");
    }

    /**
     * 获取概览页面详细信息
     */
    @PreAuthorize("@ss.hasPermi('overview:page:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        MyOverviewPage page=myOverviewPageService.selectMyOverviewPageById(id);
        MyOverviewItems param=new MyOverviewItems();
        param.setPageId(page.getId());
        List<MyOverviewItems> items= myOverviewItemsService.selectMyOverviewItemsList(param);
        page.setOverviewItemList(items);
        return AjaxResult.success(page);
    }

    @GetMapping(value = "/setPagePublic/{id}")
    public AjaxResult setPagePublic(@PathVariable("id") Long id)
    {
        MyOverviewPage page=new MyOverviewPage();
        page.setId(id);
        page.setPublicFlag("Y");
        page.setUpdateBy(SecurityUtils.getUsername());
        page.setUpdateTime(DateUtils.getNowDate());
        int result=myOverviewPageService.updateMyOverviewPage(page);
        return toAjax(result);
    }

    /**
     * 新增概览页面
     */
    @PreAuthorize("@ss.hasPermi('overview:page:add')")
    @Log(title = "概览页面", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyOverviewPage myOverviewPage)
    {
        LoginUser user= SecurityUtils.getLoginUser();
        myOverviewPage.setUserId(user.getUser().getUserId());
        return toAjax(myOverviewPageService.insertMyOverviewPage(myOverviewPage));
    }

    /**
     * 修改概览页面
     */
    @PreAuthorize("@ss.hasPermi('overview:page:edit')")
    @Log(title = "概览页面", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyOverviewPage myOverviewPage)
    {
        return toAjax(myOverviewPageService.updateMyOverviewPage(myOverviewPage));
    }

    /**
     * 删除概览页面
     */
    @PreAuthorize("@ss.hasPermi('overview:page:remove')")
    @Log(title = "概览页面", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(myOverviewPageService.deleteMyOverviewPageByIds(ids));
    }
}
